package com.lijinjiang.controller;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/cookie01")
public class Demo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 发送 Cookie
        // 1.创建 Cookie对象
        //Cookie cookie = new Cookie("username", "zs");
        // Cookie存储中文
        String value = "张三";
        value = URLEncoder.encode(value, "UTF-8");
        Cookie cookie = new Cookie("username", value);

        // 设置存活时间 1周
        cookie.setMaxAge(60 * 60 * 24 * 7);

        // 2.发送 Cookie
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
